/*//
//  jquery.lt_dialog.js
//  jq plugin
//
//  Created by sang alfred on 20/10/2013.
//  author: 昀晖、芒伶、柯织
//

// 1）table的下拉刷新、上拉刷新
// 昀晖
// 芒伶
// 柯织
// 
// 1、draw_loading_view(y);
// y小于40的时候，箭头旋转
// y大于40的时候，loading
// 
// 2、loading_view_dismiss()
// loadingview消失动画
// 
// 3、lt_table_refresh_stop(is_successed)
// 
// 	//lt_table_stop内部调用drap_up_XXXX_callback
// 	if(is_successed === yes){
// 		opts.drap_up_succ_callback();
// 	}else{
// 		opts.drap_up_failed_callback();
// 	}
// 
// 	//loading view消失动画
// 	lt_table_loading_view_dismiss();
// 
// 4、drap_up和drap_down实现
// drap_up是根据table的头距离检测
// drap_down是根据table的尾距离检测
// 
// 
// ----------------------- USEAGE -----------------------
// 
// $('table').lt_table_refresh({
// 	drap_up:function(y,this){
// 		if(y>40){
// 			....

// 			
// 			$.post('url',function(){
// 				//lt_table_stop内部调用drap_up_succ_callback
// 				//loading view消失动画
// 				$.lt_table_refresh_stop(yes,this);
// 			});
// 		}
// 	},
// 	drap_down:function(y){
// 		if(y>40){
// 			....
// 		}
// 	},
// 	drap_up_succ_callback:function(){
// 		
// 	},
// 	drap_up_failed_callback:function(){ 
// 		
// 	}
// })
// */

;(function($) {
	 
	//--------------------- public methods --------------------------
    function pp(info){
		if (window.console && window.console.log)    
			window.console.log('LOG: ' + info ); 
    }

	/* 昀晖 */
	$.lt_table_refresh_stop=function(is_successed,$this,opts) {
   		//lt_table_stop内部调用drap_up_XXXX_callback
		if(is_successed  == 'yes'){
			$this.opts.drap_up_succ_callback();
		}else{
			$this.opts.drap_up_failed_callback();
		}
	 
		//loadingview消失动画
		$.lt_table_loading_view_dismiss($this,opts);
	};
	
	/* 芒伶 */
	$.lt_table_loading_view_dismiss=function($this,opts){
		// loadingview消失动画

		// 往顶部消失
		if($this.endTopY>0){

			$('.pull_down .pull_down_load',$this).attr('class','pull_down_icon');
			$(opts.slide_name,$this).css({'-webkit-transform':'translateY('+$this.pullDownHeight+'px)','transition':'0.2s linear'});
			$('.pull_down .pull_down_label',$this).html('下拉刷新');
		}
		// 往底部消失
		if(-$this.endTopY>$this.conH-$this.winH){

			$(opts.slide_name,$this).css({'-webkit-transform':'translateY('+-($this.conH-$this.winH+$this.pullDownHeight)+'px)','transition':'0.2s linear'});
			$('.pull_up',$this).html('加载完成');

		}
	};

	// 创建下拉及加载更多
	function init_loading_view($this,opts){

		// 下拉刷新
		$(opts.slide_name,$this).prepend(
			'<div class="pull_down">' +
				'<span class="pull_down_icon"></span>' +
				'<span class="pull_down_label">下拉刷新</span>' +
			'</div>');

		// 加载更多
		$(opts.slide_name,$this).append('<div class="pull_up">加载更多</div>');

		$this.pullDownHeight = -$('.pull_down').height();
		$this.pullUpHeight = -$('.pull_up').height();
		$('.data_table').css({'-webkit-transform':'translateY('+$this.pullDownHeight+'px)'});

	}
	
	/* 昀晖 */
	function draw_loading_view(y,$this){
		// y小于40的时候，箭头旋转
		// y大于40的时候，loading
		if(-y > $this.conH-$this.winH){
			$('.pull_up' ,$this).html('松开加载');
		}
		if(y >= 0){
			$('.pull_down .pull_down_icon',$this).addClass('pull_down_rotate');
			$('.pull_down .pull_down_label',$this).html('松开刷新');
		}
		if(y <= 0){
			$('.pull_down .pull_down_icon',$this).removeClass('pull_down_rotate');
			$('.pull_down .pull_down_label',$this).html('下拉刷新');
		}
	}

	/* 柯织 */
	function pull_touchstart_event($this,opts){

		$(opts.slide_name,$this).bind('touchstart',function(e){

			// 每次手指按下的时候清空transition
			$(this).css({'transition':'none'});
			$this.startY = e.touches[0].pageY;	

			$this.conH = $(opts.slide_name,$this).height();	//获取内容区域高度;
			$this.winH = $this.height();

			$this.contentTopY = parseInt($(opts.slide_name,$this).css('-webkit-transform').replace(/[^\-0-9]/ig,""));

		})
	}
	
	/* 柯织 */
	function pull_touchmove_event($this,opts){

		$(opts.slide_name,$this).bind('touchmove',function(e){

			$this.endY = e.touches[0].pageY;
			$this.y = $this.endY-$this.startY;
			$this.moveY = $this.y+$this.contentTopY;

			$(this).css({'-webkit-transform':'translateY('+ $this.moveY+ 'px)'});

			draw_loading_view($this.moveY,$this);

			event.stopPropagation();
			event.preventDefault(); 
			return false;
		})
	}
	
	/* 柯织 */
	function pull_touchend_event($this,opts){

		$(opts.slide_name,$this).bind('touchend',function(){

			$this.endTopY = parseInt($(opts.slide_name,$this).css('-webkit-transform').replace(/[^\-0-9]/ig,""));
			$this.conH = $(opts.slide_name,$this).height();

			// 判断顶部
			if($this.winH >= $this.conH){

				$(this).css({'-webkit-transform':'translateY(0px)','transition':'0.2s linear'});

			}else{
				if($this.endTopY >= $this.pullDownHeight && $this.endTopY<0){

					$(this).css({'-webkit-transform':'translateY('+$this.pullDownHeight+'px)','transition':'0.2s linear'});

				}else if($this.endTopY >= 0){

					$(this).css({'-webkit-transform':'translateY(0px)','transition':'0.3s linear'});
					$('.pull_down .pull_down_icon',$this).attr('class','pull_down_load');
					$('.pull_down .pull_down_label',$this).html('正在加载');

					opts.drap_up($this.y,$this,opts);
				}

				// 判断底部
				if($this.endTopY <= -($this.conH-$this.winH+$this.pullDownHeight) && $this.endTopY > -($this.conH - $this.winH)){

					$(this).css(
						{'-webkit-transform':'translateY(-' +($this.conH-$this.winH+$this.pullDownHeight)+ 'px)',
							'transition':'0.3s linear'}
					);

				}else if($this.endTopY <= -($this.conH - $this.winH)){

					$(this).css(
						{'-webkit-transform':'translateY(-' +($this.conH - $this.winH)+ 'px)',
							'transition':'0.3s linear'}
					);

					$('.pull_up',$this).html('正在加载');
					opts.drap_down(-$this.y,$this,opts);
				}
			}

		})
	}
	// 检测旋转

	$(window).resize(function(){
		$this.winH = $this.height();
		$this.conH = $($this.opts.slide_name).height();

		$this.oldTopY = parseInt($(opts.slide_name,$this).css('-webkit-transform').replace(/[^\-0-9]/ig,""));

		if(-$this.moveY > $this.conH-$this.winH) {
			$($this.opts.slide_name).css(
				{'-webkit-transform':'translateY('+-($this.conH-$this.winH+$this.pullDownHeight)+'px)'}
			);
		}
	
	});

    //----------------------- jquery   plugin  -----------------------
	$.fn.lt_table_refresh = function(options) {       
        // init var
		var opts = $.extend({}, $.fn.lt_table_refresh.defaults, options);
		//start plugin here
		return this.each(function() { 
		    $this = $(this);
			$this.opts = opts;

		 	init_loading_view($this,opts);

			pull_touchstart_event($this,opts);
		    pull_touchmove_event($this,opts);	  
	        pull_touchend_event($this,opts);
		});//end this.each 
	};

    //插件的defaults    
	$.fn.lt_table_refresh.defaults = {
		slide_name: '.data_table',
		drap_up:function(y,$this,opts){
			 
		},
		drap_down:function(y,$this,opts){
			 
		},
		drap_up_succ_callback:function(){
			
		},
		drap_up_failed_callback:function(){
			
		}
    };  
})(Zepto);